var utils = require('../../utils/util.js');
var httpUrl = getApp().globalData.httpUrl;
var codeName = getApp().globalData.codeName;
var userId = getApp().globalData.userId;
Page({
  data: {
    ishis: false, //用户回复是否弹出键盘
    isSubmit: false, //键盘输入触发“发送”按钮变色
    valueName: "",//获取输入文字的长度
    cityName: "", //城市name
    cityId: ""    //城市iD
  },
  onLoad: function (e) {
    
    this.getlodeTap();
    console.log(this.data.cityName);
  },

  getlodeTap:function(e){
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        //console.log(res);
        var latitude = res.latitude
        var longitude = res.longitude
        utils.http(httpUrl + "getindex", {
          code: codeName,
          weidu: latitude,
          jingdu: longitude,
          city: wx.getStorageSync("city").cityid,
          userid: userId
        }, function (res) {
        console.log(res);
        that.setData({
          cateRes: res.cateinfo
        })
          var cateinfolist = [];
          for (var index in res.cateinfo) {
            //文字截取
            var sub = res.cateinfo[index].content.substring(0, 40);
            var subContent = sub + "...";
            that.setData({
              advlist: res.cateinfo[index].advlist
            })
            that.data.advlist.length = 3;
    
            var catePost = {
              advlist: that.data.advlist,
              subAdvlist: res.cateinfo[index].advlist, //截取后的图片
              city: res.cateinfo[index].city,
              code: res.cateinfo[index].code,
              collectby: res.cateinfo[index].collectby,
              content: res.cateinfo[index].content,
              subContent: subContent,//截取后的属性
              createTime: res.cateinfo[index].createTime,
              id: res.cateinfo[index].id,
              index: res.cateinfo[index].index,
              collect: res.cateinfo[index].collect,
              ischoiceness: res.cateinfo[index].ischoiceness,
              istop: res.cateinfo[index].istop,
              lookby: res.cateinfo[index].lookby,
              publishby: res.cateinfo[index].publishby,
              title: res.cateinfo[index].title,
              type: res.cateinfo[index].type,
              user: res.cateinfo[index].user,
              isstare: false,
              commentlist: res.cateinfo[index].commentlist,

            }
            cateinfolist.push(catePost);
          }

          if (wx.getStorageSync("city").cityid == undefined) {
            wx.setStorageSync("city", {
              cityname: res.city.name,
              cityid: res.city.id
            })
          }
          that.setData({
            bannerList: res.imagebyType1,   //轮播图
            imagebyType2: res.imagebyType2, //广告位            
            advertisemenft: res.advertisement[0], //同城入口
            cateinfo: cateinfolist, // 列表区域
            cityName: wx.getStorageSync("city").cityname,         //城市定位name
            cityId: wx.getStorageSync("city").cityid,
            model: res.model,
            houserent: res.house
          })
          console.log(res.city.name);
        })
      }
    })
  },

  //全文和收起
  noStareTap: function (e) {
    var index = e.currentTarget.dataset.index;
    this.data.cateinfo[index].isstare = !this.data.cateinfo[index].isstare;
    this.setData({
      cateinfo: this.data.cateinfo
    })
  },

  //收藏切换
  oNcollectbyTap: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    console.log(index);
    // that.data.cateinfo[index].collect = !that.data.cateinfo[index].collect;
    console.log(that.data.cateinfo[index].collect);
    var collectby="";
    if (that.data.cateinfo[index].collect==false) {
      console.log(that.data.cateinfo[index].collect+"收藏");
      collectby = parseInt(that.data.cateinfo[index].collectby)+ 1;
      that.data.cateinfo[index].collectby=collectby;
      utils.http(httpUrl + "manageCollect", {
        code: codeName,
        cateid: that.data.cateinfo[index].id,
        userid: userId
      }, function (res) {
        that.setData({
          cateinfo: that.data.cateinfo
          
        })

        that.getlodeTap();
      })
    } else {
     console.log(that.data.cateinfo[index].collect+"取消收藏");
      collectby = parseInt(that.data.cateinfo[index].collectby) - 1;
      that.data.cateinfo[index].collectby = collectby;
      utils.http(httpUrl + "manageCollect", {
        code: codeName,
        cateid: that.data.cateinfo[index].id,
        userid: userId
      }, function (res) {
        that.setData({
          cateinfo: that.data.cateinfo
        })
        that.getlodeTap();
      })
    }
  },

  //进入区域选择
  chooseLocation: function (e) {
    var cityname = e.currentTarget.dataset.cityname;
    wx.navigateTo({
      url: '../adderss/adderss?cityname=' + cityname,
    })
  },


  //进入房产和新盘
  oNtypeDetailsTap: function (e) {
    var cityid = e.currentTarget.dataset.cityid;
    var typeid = e.currentTarget.dataset.type;
    var typename = e.currentTarget.dataset.typename;
    var id = e.currentTarget.dataset.id;
    if (typeid == 1) {
      wx.navigateTo({
        url: '../house_property/house_property?cityid=' + cityid + "&id=" + id +"&typename="+typename,
      })
    }
    if (typeid == 2) {
      wx.navigateTo({
        url: '../gourmet/gourmet?cityid=' + cityid + "&id=" + id+"&typename="+typename,
      })
    }
    if (typeid == 3) {
      wx.navigateTo({
        url: '../contact_us/contact_us?id' + id+"&typename="+typename,
      })
    }

  },

  //点击头像进入个人主页
  onPersonalHomeTap: function (e) {
    wx.navigateTo({
      url: '../personal_homepage/personal_homepage',
    })
  },

  //联系他
  onPhoneTap: function (e) {
    var phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
    });
  },

  //点击头像进入个人主页
  onPersonalHomeTap: function (e) {
    var modelid = e.currentTarget.dataset.modelid;
    wx.navigateTo({
      url: '../gourmet/gourmet_details/gourmet_details?modelid=' + modelid,
    })
  },
  //进入入驻商圈
  oNbusinessTap:function(e){
    wx.navigateTo({
      url: '../business_settled/business_settled',
    })
  },
  //键盘输入是触发按钮
  oNbindinputTap: function (e) {
    var valueName = e.detail.value;
    if (valueName.length == 0) {
      this.data.isSubmit = false;
    } else {
      this.data.isSubmit = true;
    }
    this.setData({
      isSubmit: this.data.isSubmit,
      valueName: valueName
    })

  },

  //点击回复信息小图标
  oNinfonumTap: function (e) {
    var cateid = e.currentTarget.dataset.cateid;
    var nickname = e.currentTarget.dataset.nickname;
    var type = e.currentTarget.dataset.type;
    this.setData({
      ishis: true,
      cateid: cateid,
      nickname: nickname,
      type: type,
      valueName: "",
      username: "",
    })
  },

  //点击不同用户回复信息
  onchangeTap: function (e) {
    var that = this;
    var cateid = e.currentTarget.dataset.cateid;
    var parentusername = e.currentTarget.dataset.parentusername;
    var nickname = e.currentTarget.dataset.nickname;
    var parentid = e.currentTarget.dataset.parentid;
    var parentuserid = e.currentTarget.dataset.parentuserid;
    var username = e.currentTarget.dataset.username;   //要回复的username
    var type = e.currentTarget.dataset.type;
    var userid = e.currentTarget.dataset.userid;
    var id = e.currentTarget.dataset.id;
    if (parentuserid == undefined) {
      that.setData({
        parentid: id,
        parentuserid: userid,
      })
    } else {
      that.setData({
        parentid: parentid,
        parentuserid: parentuserid,
      })
    }

    this.setData({
      ishis: true,
      cateid: cateid,
      parentusername: parentusername,
      nickname: nickname,
      type: type,
      username: username,
      valueName: ""
    })
  },
  //提交回复
  bindsubmit: function (e) {
    var that = this;
    if (that.data.type == 1) {
      utils.http(httpUrl + "publishComment", {
        userid: userId, //	用户id
        cateid: that.data.cateid,//美食id
        code: codeName,//code值
        content: that.data.valueName, //	评论内容
        username: that.data.nickname  // 发布的用户的名字
      }, function (res) {
        console.log(res);
        that.getlodeTap();
      })
    } else {
      utils.http(httpUrl + "publishComment", {
        userid: userId, //	用户id
        cateid: that.data.cateid,//美食id
        code: codeName,//code值
        content: that.data.valueName, //	评论内容
        username: that.data.nickname,// 发布的用户的名字
        parentid: that.data.parentid,  //评论的ID
        parentuserid: that.data.parentuserid,
        parentusername: that.data.username
      }, function (res) {
        console.log(res);
        that.getlodeTap();
      })
    }
  },
  //formSubmit  跳入商家列表页
  formSubmit:function(e){
    var name=e.detail.value.name;
    wx.setStorageSync("name", name)
    wx.switchTab({
      url: '../business/business'
    })
  },
  //滑动键盘收起
  handletouchmove: function (e) {
    this.setData({
      ishis: false,
    })
  },
  //转发
  onShareAppMessage: function (res) {
    return {
      title: '同城信息',
      path: '/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  //房产出售详情
  oNhouseDetailsTap: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../house_property/houseDetails/houseDetails?id=' + id,
    })
  },

    //进入商家详情页
  oNbusinessDetailsTap: function (e) {
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '../business/businessDetails/businessDetails?id=' + id + "&name=" + name
    })
  }
})